﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Contoso;
using Contoso.DAL;

namespace Contoso.Controllers
{ 
    public class StudentController : Controller
    {
        private SchoolContext db = new SchoolContext();


        private IStudentRepository studentRepository;


        public StudentController()
        {
            studentRepository = new StudentRepository(new SchoolContext());
        }

        //
        // GET: /Student/

        public ViewResult Index(string sortOrder, string searchStr)
        {
            ViewBag.NameSortParm = string.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DataSortParm = sortOrder == "Date" ? "Date desc" : "Date";
  

            var students = from s in  studentRepository.GetStudents()
                           select s;
            switch (sortOrder)
            {
                case "Name desc":
                    students = students.OrderByDescending(s => s.LastName);
                    break;
                case "Date":
                    students = students.OrderBy(s => s.EnrollmentDate);
                    break;
                case "Date desc":
                    students = students.OrderByDescending(s => s.EnrollmentDate);
                    break;
                default:
                    students = students.OrderBy(s => s.LastName);
                    break;
            }

            if (!string.IsNullOrEmpty(searchStr))
            {
             students =  students.Where(o => o.LastName.ToLower().Contains(searchStr.ToLower()) ||
                 o.FirstMidName.ToLower().Contains(searchStr.ToLower()));
            }
            return View(students.ToList());
        }

        //
        // GET: /Student/Details/5

        public ViewResult Details(int id)
        {
            var students = db.Students.Include(x=>x.Enrollments);
            Student student = (from item in students
                               where item.StudentID == id
                               select item).FirstOrDefault();
           // db.Entry(student).Collection(x => x.Enrollments).Load();  
            return View(student);
        }

        //
        // GET: /Student/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                //db.Students.Add(student);
               // db.SaveChanges();
                studentRepository.InsertStudent(student);
                studentRepository.Save();
                return RedirectToAction("Index");  
            }

            return View(student);
        }
        
        //
        // GET: /Student/Edit/5
 
        public ActionResult Edit(int id)
        {
            Student student = db.Students.Find(id);
            return View(student);
        }

        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /Student/Delete/5
 
        public ActionResult Delete(int id)
        {
            Student student = db.Students.Find(id);
            return View(student);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
           // Student student = studentRepository.GetStudentByID(id);
           // db.Students.Remove(student); 
            //db.SaveChanges();
            studentRepository.DeleteStudent(id);
            studentRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}